package com.zg.api.service.impl;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zg.api.service.ApiCategoryService;
import com.zg.api.sys.ApiE;
import com.zg.common.core.dao.IDao;
import com.zg.common.core.dao.apis.ApiCategoryDao;
import com.zg.common.core.dao.apis.entity.ApiCategory;
import com.zg.common.core.service.PageBaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

/**
 * <p>Description: </p>
 *
 * @author zhanfeng.liang
 * @since 2025/6/16 20:11
 */

@Slf4j
@Service
public class ApiCategoryServiceImpl extends PageBaseService<ApiCategory, ApiCategoryDao> implements ApiCategoryService {


//    @Autowired
//    @Qualifier("apiCategoryDao")
//    @Override
//    protected void setDao(IDao<ApiCategory> dao) {
//        this.dao = dao;
//    }

    @Override
    public  LambdaQueryWrapper<ApiCategory> buildQueryWrapper(ApiCategory apiCategory){
        LambdaQueryWrapper<ApiCategory> qw = new LambdaQueryWrapper<>();
        if(apiCategory!=null){
            qw.like(!StrUtil.isEmpty(apiCategory.getCategoryName()), ApiCategory::getCategoryName, apiCategory.getCategoryName());
        }
        return qw;
    }

    public ApiCategory getByName(String name){
        LambdaQueryWrapper<ApiCategory> qw = new LambdaQueryWrapper<>();
        qw.eq(ApiCategory::getCategoryName,name);
        return baseDao.getOne(qw);
    }

    @Override
    protected void preAdd(ApiCategory source) {
        ApiCategory category = getByName(source.getCategoryName());
        if(category!=null){
            ApiE.CATEGORY_IS_EXIST.happen();
        }
    }
}
